package eclipse.lib.sound;
import eclipse.util.*;
import eclipse.util.io.IOTools;
/** 
 * AudioDevice represents a collective playback utility that the Client uses to play audio media.
 * This currently support midi, mp3, and wav. Note: you cannot use JavaZoom if you charge money per copy.
 */
/*
 * Copyright (c) 2009, TOD Productions All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 *	- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 *	- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 *	-Neither the name of the organization nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */
public class AudioDevice 
{	
	private enum CodecType {mp3, mid, wav};
	private SoundPlayer wavCodec;
	private MidiPlayer	midCodec;
	private MpegPlayer	mp3Codec;
	private CodecType 	codecType;	
	/** Creates the Audio Playback device. */
	public AudioDevice()
	{
		wavCodec 	= new SoundPlayer();
		midCodec 	= new MidiPlayer();
		mp3Codec	= new MpegPlayer();		
	}
	/**
	 * Loads an Audio file to the device.
	 * @param sFile String path of the File in the form of an ERL string.
	 */
	public void loadAudio(String sFile)
	{
		if(		sFile.endsWith("mp3"))
		{			
			codecType = CodecType.mp3;
			mp3Codec.loadMP3(new ERL(sFile));			
		}
		else if(sFile.endsWith("mid"))
		{
			codecType = CodecType.mid;
			midCodec.loadMidi(new ERL(sFile));
		}
		else if(sFile.endsWith("wav"))
		{
			codecType = CodecType.wav;
			wavCodec.loadAudio(new ERL(sFile));
		}		
		else if(sFile.endsWith("m3u"))
		{			
			String m3u;
			if(sFile.startsWith("jar://") || sFile.startsWith("http://"))
			{
				String tmp = System.nanoTime() + ".m3u";
				IOTools.download(new ERL(sFile), tmp);
				IOTools.setFileAsTemporary(tmp);
				m3u = IOTools.readAsString(tmp);
			}
			else
				m3u = IOTools.readAsString(sFile);			
			codecType = CodecType.mp3;
			mp3Codec.loadMP3(new ERL(m3u));
		}
	}	
	/** Plays the audio one time */
	public void playAudio()
	{
		if(		codecType == CodecType.mp3)		
			mp3Codec.playMP3();		
		else if(codecType == CodecType.mid)
				midCodec.playMidi();
		else if(codecType == CodecType.wav)
				wavCodec.playAudio();
	}
	/** Loops the audio until told to stop. */
	public void loopAudio()
	{
		if(		codecType == CodecType.mp3)		
			mp3Codec.loopMP3();			
		else if(codecType == CodecType.mid)
				midCodec.loopMidi();
		else if(codecType == CodecType.wav)
				wavCodec.playAudio();
	}
	/** Stops the playback of audio */
	public void stopAudio()
	{
		if(		codecType == CodecType.mp3)
				mp3Codec.stopMP3();
		else if(codecType == CodecType.mid)
				midCodec.stopMidi();
		else if(codecType == CodecType.wav)
				wavCodec.stopAudio();
	}
}
